package com.atguigu.gmall.index.service.impl;

import com.agtuigu.gmall.pms.entity.CategoryEntity;
import com.agtuigu.gmall.pms.vo.CategoryVo;
import com.alibaba.fastjson.JSON;
import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.index.annotation.GmallCache;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.index.service.IndexService;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private GmallPmsClient gmallPmsClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    private static final String KEY_PREFIX = "index:cates:";

    @Override
    public List<CategoryEntity> queryLvl1Categories() {
        Resp<List<CategoryEntity>> listResp = gmallPmsClient.queryCategoriesByPidOLevel(1, null);
        return listResp.getData();
    }

    public void testLock() {
//        RLock lock = redissonClient.getLock("lock");
//        lock.lock();
        String numString = redisTemplate.opsForValue().get("num");
        if (StringUtils.isEmpty(numString)) {
            return;
        }
        int num = Integer.parseInt(numString);
        redisTemplate.opsForValue().set("num", String.valueOf(++num));
//        lock.unlock();
    }

    @GmallCache(prefix = "index:cates:",timeout =7200,random = 100)
    @Override
    public List<CategoryVo> querySubCategories(Long pid) {
//        //1.判断缓存中有没有数据
//        String cateJson = redisTemplate.opsForValue().get(KEY_PREFIX + pid);
//        //2.有，直接返回
//        if (!StringUtils.isEmpty(cateJson)) {
//            return JSON.parseArray(cateJson, CategoryVo.class);
//        }
//
//
//        RLock lock = redissonClient.getLock("lock" + pid);
//        //手动锁(加锁)
//        lock.lock();
//        //1.判断缓存中有没有数据
//        String cateJson2 = redisTemplate.opsForValue().get(KEY_PREFIX + pid);
//        //2.有，直接返回
//        if (!StringUtils.isEmpty(cateJson2)) {
//            //释放锁
//            lock.unlock();
//            return JSON.parseArray(cateJson2, CategoryVo.class);
//        }
        //查询数据库
        Resp<List<CategoryVo>> listResp = gmallPmsClient.querySubCategories(pid);
//        List<CategoryVo> categoryVos = listResp.getData();
        //3.查询完成后放入缓存
        //redisTemplate.opsForValue().set(KEY_PREFIX + pid, JSON.toJSONString(categoryVos), 7 + new Random().nextInt(5), TimeUnit.DAYS);
        //释放锁
//        lock.unlock();
        return listResp.getData();
    }
}
